<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="serach" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="ordertableData" border style="width: 100%">
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="500px">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80px">
        </el-table-column>
        <el-table-column label="是否付款" width="80px">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay == '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80px">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160px">
          <template slot-scope="scope">
            <el-button type="primary" class="el-icon-edit" size="mini" @click="editcitydata(scope.row.user_id)">
            </el-button>
            <el-button type="success" class="el-icon-s-opportunity" size="mini" @click="transmits(scope.row.order_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 7, 10, 15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editdialogVisible" width="50%">
      <!-- 级联选择 加上表单-->
      <el-form :model="editruleForm" :rules="editrules" ref="editruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader v-model="editruleForm.address1" :options="citydata" :props="{ expandTrigger: 'hover' }"
            @change="handleChange" class="cas_wd"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editruleForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editreset">取 消</el-button>
        <el-button type="primary" @click="editresult">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看物流的对话框 -->
  <el-dialog
  title="物流信息"
  :visible.sync="transmitdialogVisible"
  width="30%"
  >
  <!-- 物流信息 -->
    <el-timeline>
    <el-timeline-item
      v-for="(activity, index) in activities"
      :key="index"
      :timestamp="activity.timestamp">
      {{activity.content}} 1
    </el-timeline-item>
  </el-timeline>
</el-dialog>

  </div>
</template>

<script>
  import citydata from './citydata.js'
  export default {
    data() {
      return {
        // 搜索框
        serach: '',
        ordertableData: [],
        // 请求的参数
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 7,
          user_id: '',
          pay_status: '',
          is_send: '',
          order_fapiao_title: '',
          order_fapiao_company: '',
          order_fapiao_content: '',
          consignee_addr: ''
        },
        // 总条数
        total: 0,
        // 这是城市的数据信息
        citydata: citydata,
        editdialogVisible: false,
        editruleForm: {
          address1: '',
          address2: ''
        },
        editrules: {
          address1: [{
            required: true,
            message: '请选择地址',
            trigger: 'blur'
          }],
          address2: [{
            required: true,
            message: '请选择地址',
            trigger: 'blur'
          }]
        },
        // 要修改的订单的id
        ids: '',
        // queryInfoedit: {
        //   query: '',
        //   pagenum: 1,
        //   pagesize: 7,
        //   user_id: this.ids,
        //   pay_status: '',
        //   is_send: '',
        //   order_fapiao_title: '',
        //   order_fapiao_company: '',
        //   order_fapiao_content: '',
        //   consignee_addr: this.editruleForm.address1 + this.editruleForm.address2
        // }
        queryInfoedit: {
          query: '',
          pagenum: 1,
          pagesize: 7,
          user_id: this.ids,
          pay_status: '',
          is_send: '',
          order_fapiao_title: '',
          order_fapiao_company: '',
          order_fapiao_content: '',
          consignee_addr: this.editruleForm
        },
        transmitdialogVisible: false,
        // 物流数据
         activities: [{
          content: '活动按期开始',
          timestamp: '2018-04-15'
        }, {
          content: '通过审核',
          timestamp: '2018-04-13'
        }, {
          content: '创建成功',
          timestamp: '2018-04-11'
        }]
      }
    },
    created() {
      this.getlist()
    },
    methods: {
      async getlist() {
        const {
          data: res
        } = await this.$http.get('orders', { params: this.queryInfo })
        //  console.log(res)
        if (res.meta.status !== 200) {
          return this.$message.error('获取订单失败')
        }
        this.ordertableData = res.data.goods
        this.total = res.data.total
      },
      // 页面显示数据大小发生变小
      handleSizeChange(newSize) {
        // console.log(newSize);
        this.queryInfo.pagesize = newSize
        this.getlist()
      },
      // 切换到其他页时触发
      handleCurrentChange(newPage) {
        // console.log(newPage);
        this.queryInfo.pagenum = newPage
        this.getlist()
      },
      // 修改地址
      editcitydata(info) {
        this.editdialogVisible = true
        this.ids = info
      },
      // 级联选择框触发事件
      handleChange() {},
      // 修改取消
      editreset() {
        this.$refs.editruleForm.resetFields()
        this.editdialogVisible = false
      },
      // 确定修改
      async editresult() {
        console.log(this.ids)
        // 进行预验证
        this.$refs.editruleForm.validate(async validate => {
          if (!validate) {
            return this.$message.error('修改失败')
          }
          // 验证成功
          const {
            data: res
          } = await this.$http.get('orders', {
          params: {
          query: '',
          pagenum: 1,
          pagesize: 7,
          user_id: this.ids,
          pay_status: '',
          is_send: '',
          order_fapiao_title: '',
          order_fapiao_company: '',
          order_fapiao_content: '',
          consignee_addr: this.editruleForm.address1 + this.editruleForm.address2
            }
          })
          if (res.meta.status !== 200) {
            return this.$message.error('修改失败')
          }
          this.$message.success('修改成功')
          // console.log(res)
          this.getlist()
          this.editdialogVisible = false
        })
      },
      // 点击查看物流信息
     async transmits(id) {
        this.transmitdialogVisible = true
        // 获取物流信息
        // console.log(id)
      //  const { data: res } = await this.$http.get(`kuaidi/${id}`)
      //  console.log(res)
      }
    }
  }

</script>

<style lang="less" scoped>
  .cas_wd {
    width: 300px !important;
  }

</style>
